package com.ncloud.system.mapper;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ncloud.system.entity.User;

@Mapper
public interface UserMapper {

	@Select("select user_id,company_id,depart_id,account,passwd,icon,name,contact_phone,state,first_login_time,"
			+ "superman,last_login_time,remarks from sys_user where account=#{account} and state <>'0'")
	@Results({ @Result(column = "user_id", property = "userId"), @Result(column = "company_id", property = "companyId"),
			@Result(column = "depart_id", property = "departId"), @Result(column = "account", property = "account"),
			@Result(column = "passwd", property = "passwd"), @Result(column = "icon", property = "icon"),
			@Result(column = "name", property = "name"), @Result(column = "superman", property = "superman"),
			@Result(column = "contact_phone", property = "contactPhone"), @Result(column = "state", property = "state"),
			@Result(column = "last_login_time", property = "lastLoginTime"),@Result(column = "first_login_time", property = "firstLoginTime"),
			@Result(column = "remarks", property = "remarks") })
	User findByUsername(String account);
	
	@Select("select user_id,company_id,depart_id,account,passwd,icon,name,contact_phone,state,"
			+ "superman,last_login_time,remarks from sys_user where user_id=#{userId} and state <>'0'")
	@Results({ @Result(column = "user_id", property = "userId"), @Result(column = "company_id", property = "companyId"),
			@Result(column = "depart_id", property = "departId"), @Result(column = "account", property = "account"),
			@Result(column = "passwd", property = "passwd"), @Result(column = "icon", property = "icon"),
			@Result(column = "name", property = "name"), @Result(column = "superman", property = "superman"),
			@Result(column = "contact_phone", property = "contactPhone"), @Result(column = "state", property = "state"),
			@Result(column = "last_login_time", property = "lastLoginTime"),
			@Result(column = "remarks", property = "remarks") })
	User findByUserId(Long userId);
	
	@Select("select user_id,company_id,depart_id,account,passwd,icon,name,contact_phone,state,"
			+ "superman,last_login_time,remarks from sys_user where depart_id=#{departId} and state <>'0'")
	@Results({ @Result(column = "user_id", property = "userId"), @Result(column = "company_id", property = "companyId"),
			@Result(column = "depart_id", property = "departId"), @Result(column = "account", property = "account"),
			@Result(column = "passwd", property = "passwd"), @Result(column = "icon", property = "icon"),
			@Result(column = "name", property = "name"), @Result(column = "superman", property = "superman"),
			@Result(column = "contact_phone", property = "contactPhone"), @Result(column = "state", property = "state"),
			@Result(column = "last_login_time", property = "lastLoginTime"),
			@Result(column = "remarks", property = "remarks") })
	List<User> findByDepartId(Long departId);

	@Update("update sys_user set state = #{state} where user_id = #{userId}")
	void changeUserState(@Param("userId")Long userId, @Param("state")String state);

	@Update("update sys_user set first_login_time = #{firstLoginTime} where user_id = #{userId}")
	void updateFirstLoginTime(@Param("userId")Long userId, @Param("firstLoginTime")Date firstLoginTime);

	@Select("select count(1) count from sys_user_role where role_id=#{roleId} and now() between eff_date and exp_date")
	int getRoleUsers(Long roleId);
	@Update("update sys_user set passwd=#{password} where user_id = #{userId}")
	void updatePassword(@Param("userId")Long userId, @Param("password")String password);

	@Select("select a.user_id userId,a.account userName,cast(b.company_id as char) companyId,b.company_name companyName,cast(c.depart_id as char) departId,c.depart_name departName,"
			+ "(select count(role_id) from sys_user_role where user_id = a.user_id and now() between eff_date and exp_date) roleNum,"
			+ "a.name,a.contact_phone contactPhone,a.state from sys_user a,sys_company b,sys_depart c "
			+ " where a.company_id = b.company_id and a.depart_id = c.depart_id and a.state<>'0' and c.use_tag='1'"
			+ " and (a.company_id= if(length(#{companyId})>0,#{companyId},a.company_id)) and (a.depart_id=if(length(#{departId})>0,#{departId},a.depart_id))"
			+ " and (a.account like if(length(#{account})>0,concat('%',#{account},'%'),a.account))"
			+ " and exists(select 1 from sys_user_role where user_id = a.user_id and role_id = ifnull(#{roleId},role_id) and NOW() BETWEEN eff_date and exp_date)")
	List<Map<String, Object>> getUsers(@Param("companyId")Long companyId, @Param("departId")Long departId,
			@Param("roleId")Long roleId, @Param("account")String account);

	@Update("update sys_user set icon = #{filePath} where user_id =#{userId}")
	void setUserHead(@Param("userId")Long userId, @Param("filePath")String filePath);

	@Update("update sys_user set state ='0' where user_id = #{userId}")
	void deleteUser(Long userId);

	@Insert("insert into sys_user(user_id, company_id, depart_id, account, passwd, icon, name,"
			+ " contact_phone, state, create_user, create_date,update_user,update_date, remarks)"
			+ " values(#{userId}, #{companyId}, #{departId}, #{account}, #{passwd}, #{icon}, #{name},"
			+ " #{contactPhone}, #{state}, #{createUser}, #{createDate},#{updateUser}, #{updateDate}, #{remarks})")
	void save(User user);

	@Update("update sys_user set depart_id=#{departId}, name=#{name}, contact_phone=#{contactPhone},"
			+ " remarks = #{remarks} where user_id = #{userId} and state <> '0'")
	void updateUser(User user);

	@Update("update sys_user set state='0' where depart_id = #{departId} and state <>'0'")
	void deleteuserByDepartId(Long departId);

}
